package com.meritdata.cloud.data.service.development.controller;

import com.meritdata.cloud.data.service.development.model.DataApiCategory;
import com.meritdata.cloud.data.service.development.model.TdDataApiParameter;
import com.meritdata.cloud.data.service.development.service.DataApiCategoryService;
import com.meritdata.cloud.data.service.development.service.DataApiParameterService;
import com.meritdata.cloud.resultmodel.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@RequestMapping("/apiParameter")
@Controller
public class DataApiParameterController {

    @Autowired
    private DataApiParameterService service;

    @RequestMapping("/tree")
    @ResponseBody
    public ResultBody tree(@RequestParam String apiId) {
        ResultBody r = new ResultBody();
        r.setData(service.tree(Long.valueOf(apiId)));
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/create")
    @ResponseBody
    public ResultBody create(@RequestParam String name,@RequestParam String code,
                             @RequestParam String type,@RequestParam String defaultVal,@RequestParam String apiId) {
        ResultBody r = new ResultBody();
        TdDataApiParameter d = new TdDataApiParameter();
        d.setName(name);
        d.setCode(code);
        d.setType(type);
        d.setApiId(Long.valueOf(apiId));
        d.setDefaultVal(defaultVal);
        d.setUpdateTime(new Date());
        d.setCreateTime(new Date());
        service.save(d);
        r.setData(d);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResultBody update(@RequestParam String name,@RequestParam String code,
                             @RequestParam String type,@RequestParam String defaultVal, @RequestParam String id) {
        ResultBody r = new ResultBody();
        r.setData(service.udpate(id,name,code,type,defaultVal,new Date()));
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/del")
    @ResponseBody
    public ResultBody del(@RequestParam String id) {
        ResultBody r = new ResultBody();
        r.setData(service.delete(id));
        r.setSuccess(true);
        return r;
    }
}